/**
 * 通用 AI API 适配器接口
 * 定义所有 AI API 适配器必须实现的标准接口
 */

export enum ApiProvider {
  ANTHROPIC = 'anthropic',
  OPENAI = 'openai',
  GEMINI = 'gemini',
  COHERE = 'cohere'
}

export enum TargetProvider {
  COPILOT = 'copilot',
  AZURE_OPENAI = 'azure-openai',
  CUSTOM = 'custom'
}

// 通用消息接口
export interface GenericMessage {
  role: 'system' | 'user' | 'assistant';
  content: string | GenericContentBlock[];
}

export interface GenericContentBlock {
  type: 'text' | 'image' | 'tool_use' | 'tool_result';
  text?: string;
  source?: any;
  name?: string;
  input?: any;
  tool_use_id?: string;
  content?: any;
}

// 通用请求接口
export interface GenericRequest {
  model: string;
  messages: GenericMessage[];
  max_tokens?: number;
  temperature?: number;
  top_p?: number;
  stream?: boolean;
  tools?: GenericTool[];
  tool_choice?: any;
  system?: string | GenericContentBlock[];
  metadata?: Record<string, any>;
}

// 通用响应接口
export interface GenericResponse {
  id: string;
  model: string;
  content: GenericContentBlock[];
  stop_reason: string;
  usage: {
    input_tokens: number;
    output_tokens: number;
    total_tokens: number;
  };
  metadata?: Record<string, any>;
}

// 通用工具接口
export interface GenericTool {
  name: string;
  description: string;
  input_schema: any;
}

// 健康检查接口
export interface HealthStatus {
  status: 'healthy' | 'degraded' | 'unhealthy';
  provider: ApiProvider;
  target: TargetProvider;
  details?: Record<string, any>;
  lastCheck: Date;
}

// Token 管理接口
export interface TokenManager {
  getValidToken(): Promise<string>;
  refreshToken(): Promise<void>;
  isTokenValid(): boolean;
  getTokenInfo(): any;
  clearToken(): void;
}

/**
 * AI API 适配器基础接口
 * 所有具体的 API 适配器都必须实现此接口
 */
export interface ApiAdapter {
  readonly provider: ApiProvider;
  readonly target: TargetProvider;
  
  /**
   * 将源 API 格式转换为通用格式
   */
  convertToGeneric(sourceRequest: any): Promise<GenericRequest>;
  
  /**
   * 将通用格式转换为目标 API 格式
   */
  convertFromGeneric(genericRequest: GenericRequest): Promise<any>;
  
  /**
   * 将目标 API 响应转换为通用格式
   */
  parseTargetResponse(targetResponse: any, originalRequest?: any): Promise<GenericResponse>;
  
  /**
   * 将通用响应转换为源 API 格式
   */
  convertToSourceResponse(genericResponse: GenericResponse, originalRequest?: any): Promise<any>;
  
  /**
   * 获取请求头部
   */
  getRequestHeaders(config: any): Promise<Record<string, string>>;
  
  /**
   * 验证请求格式
   */
  validateRequest(request: any): Promise<boolean>;
  
  /**
   * 获取目标 API 端点
   */
  getTargetEndpoint(operation: string): string;
  
  /**
   * 健康检查
   */
  checkHealth(): Promise<HealthStatus>;
  
  /**
   * 获取 Token 管理器（如果需要）
   */
  getTokenManager?(): TokenManager;
}

/**
 * 适配器注册表接口
 */
export interface AdapterRegistry {
  register(adapter: ApiAdapter): void;
  getAdapter(provider: ApiProvider, target: TargetProvider): ApiAdapter | null;
  getAllAdapters(): ApiAdapter[];
  getAdaptersByProvider(provider: ApiProvider): ApiAdapter[];
  getAdaptersByTarget(target: TargetProvider): ApiAdapter[];
}

/**
 * 转发请求上下文
 */
export interface ForwardingContext {
  requestId: string;
  provider: ApiProvider;
  target: TargetProvider;
  originalRequest: any;
  userInfo?: {
    ip: string;
    userAgent: string;
    headers: Record<string, string>;
  };
  config: any;
  startTime: number;
}

/**
 * 转发结果
 */
export interface ForwardingResult {
  success: boolean;
  response?: any;
  error?: Error;
  context: ForwardingContext;
  endTime: number;
  duration: number;
  metadata?: Record<string, any>;
}